<?php
/**
 * Created by PhpStorm.
 * User: kwibon
 * Date: 2018/10/29
 * Time: 12:19
 */

namespace app\admin\controller;
use think\Db;
use \think\Cookie;
use app\admin\model\Classify as ClassifyModel;
class Taglist extends User
{
    /**
     * wap前端横排list列表
     */
    public function index()
    {
        $classify = Db::name('classify')->select();
        foreach ($classify as $k=>$v) {
            if(!$v['updatetime']){
                $classify[$k]['updatetime'] = '-';
            }else{
                $classify[$k]['updatetime'] = date("Y-m-d H:i:s",$v['updatetime']);
            }
        }

        $this->assign('classify', $classify);
        return $this->fetch();
    }

    /**
     * 修改Classify
     */
    public function addclassify()
    {
        $id = input('id',0);
        if(empty($id)) return $this->error('参数有误');
        $info = db('classify')->find($id);
        if(empty($info)) return $this->error('数据有误');
        $this->assign('info', $info);
        return $this->fetch();
    }


    /**
     * 禁用/启用Classify
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $model = new ClassifyModel();
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            $status = $this->request->has('status') ? $this->request->param('status', 0, 'intval') : 0;
            if($status){
                $data['classify_status'] =$status;
            }
            if($status==2){
                $msg='禁用';
            }else{
                $msg='启用';
            }
            $data['updatetime']=time();
            if (false == $model->allowField(true)->save($data, ['classify_id' => $id])) {
                return $this->error($msg.'失败');
            } else {
                return $this->success($msg.'Classify成功', 'admin/taglist/index');
            }
        }
    }

    ///////////////////////// 20200522 补充/////////////////////
    //新增poi分类
    public function createClassify()
    {
        //直接渲染页面
        if(!request()->isPost()){
            return $this->fetch();
        }
        //提交操作
        $post = request()->post();
        if(empty($post['classify_name'])) return $this->error('请填写栏目名称');
        if(empty($post['classify_en_name'])) return $this->error('请填写栏目英文名称');
        $classify = db('classify')->where('classify_name',$post['classify_name'])->find();
        if(!empty($classify)) return $this->error('栏目名称已存在，请更换');

        $post['classify_status'] = '2';//默认隐藏
        $post['is_use'] = 1;
        $post['creattime'] = $post['updatetime'] = time();
        $res = db('classify')->insert($post);
        if($res){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }

    }
    //修改分类的状态
    public function updateClassifyData()
    {
        $post = request()->post();
        $id = $post['id']??0;
        if(empty($id)) return $this->error('参数有误');
        $classify = db('classify')->find($id);
        if(empty($classify)) return $this->error('数据有误');

        unset($post['id']);
        $post['updatetime'] = time();
        $res = db('classify')->where('classify_id',$id)->update($post);
        if($res){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }

    }
}
